/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using Core.Model.Data.Variables;
using Core.Model.Data.Generics;

namespace Core.Model.Actions.Elementary
{
    public class SetVariable : ParameterAction
    {
        #region Constructors

        public SetVariable ( GenericObjectRef targetRef ) : base( targetRef ) { }
        public SetVariable () : base() { }

        #endregion

        #region Actions

        internal override void Run ()
        {
            if ( Target is BooleanVariable )
                ( ( BooleanVariable ) Target ).Value = BoolVal.Value;
            else if ( Target is ContentRefVariable )
                ( ( ContentRefVariable ) Target ).Value = ContentRefVal.Value;
            else if ( Target is IntegerVariable )
            {
                if ( IntVal != null )
                    ( ( IntegerVariable ) Target ).Value = IntVal.Value;
                else if ( StringVal != null )
                {
                  //  try
                 //   {
                    System.Text.RegularExpressions.Regex filterAlpa = new System.Text.RegularExpressions.Regex( "[^0-9]" );
                    String var = filterAlpa.Replace( StringVal.Value, "" );
                    
                    if(var != "")
                        ( ( IntegerVariable ) Target ).Value = int.Parse( var );
                 //   }
                 //   catch ( FormatException e )
                 //   {
                  //      ( ( IntegerVariable ) Target ).Value = 0;
                  //      log.Warn( "Set Variable: String => Int. String wrong format ("+StringVal.Value+").", e );
                  //  }
                }
                else throw new Exception( "Dunno type" );
                
            }
            else if ( Target is OctetStringVariable ){

                if (StringVal != null)
                ( ( OctetStringVariable ) Target ).Value = StringVal.Value;
                else if (IntVal != null)
                    ( ( OctetStringVariable ) Target ).Value = IntVal.Value.ToString();
                else throw new Exception("Dunno type");
            }
            else if ( Target is ObjectRefVariable )
                ( ( ObjectRefVariable ) Target ).Value = ObjectRefVal.Value;
        }
        #endregion
    }
}
